// pages/packageA//selfdefine-option/selfdefine-option.js
var util = require('../../utils/util.js')
const db = wx.cloud.database()
const app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    
    manager:[],
    energy: [
      { name: 'live', value: '常住' },
    ],
    leavestyle: ['因公', '因私', '其他'],
    leaveway:['自驾', '火车', '飞机','长途巴士','其他'],
    options: [{
      "value": "",
      "value_time": "",
      "value_leavetime": "",
      "value_region": []
    }], //选项列表
    energySelect:"",
    username: "",
    haha: "",
    phonenumber: "",
    multiArray: [
      [''],
      [""]
    ],
    multiIndex: [0, 0],
    workshop: 'PFE1P',
    line: 'ZKG1',
    region: ['广东省', '广州市', '海珠区'],
  },

  /**
   * 生命周期函数--监听页面加载
   */

  onLoad: function (options) {
    var HAHA = "nile";
    var person = {
      firstName: "John",
      lastName : this.onLoad.HAHA,
      id     : 5566,
      fullName : function() {
        return this.firstName + " " + this.lastName
      }
    };
    console.log(this.HAHA)

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  usernameInput: function (e) {
    var that = this
    // this.data.username = e.detail.value
    that.setData({
    username:e.detail.value,
  })
    for (let key in that.data.manager){
      console.log(key)
      if  (that.data.username === that.data.manager[key]){
      that.setData({
        openid_excel_true: true
      })
    }
    }
  },
  phonenumberInput: function (e) {
    var that = this;
    that.setData({
      phonenumber:e.detail.value,
    })
    console.log(this.data.phonenumber)
  },
  checkboxChange: function (e) {
    console.log('checkbox发生change事件，携带value值为：', e.detail.value)
    this.setData({
      energySelect: e.detail.value
    })
    console.log('checkbox发生change事件，携带value值为：', this.data.energySelect)
  },
  bindblur: function (e) {
    console.log(e)
  },
  //添加选项
  addOption(e) {
    if (this.data.options.length > 3) {
      wx.showToast({
        icon: "none",
        title: '目的地数量过多，请增加表单',
        duration: 2000
      })
    } else {
      let options = this.data.options
      options.push({
        value: ''
      })
      this.setData({
        options: options
      })
      console.log(this.data.options)
    }
  },
  //文本输入
  bindinput(e) {
    console.log(e)
    let index = e.currentTarget.dataset.id //索引位置
    let value = e.detail.value //输入的内容
    let options = this.data.options
    options[index].value_region = value
    this.setData({
      options: options
    })
    console.log("asdfasd", this.data.options)
  },
  bindDateChange(e) {
    console.log(e)
    let index = e.currentTarget.dataset.id //索引位置
    let value = e.detail.value //输入的内容
    let options = this.data.options
    options[index].value_time = value
    this.setData({
      options: options
    })
    // console.log(this.data.options)
  },
  bindDateChange_leave(e) {
    console.log(e)
    let index = e.currentTarget.dataset.id //索引位置
    let value = e.detail.value //输入的内容
    let options = this.data.options
    options[index].value_leavetime = value
    this.setData({
      options: options
    })
    console.log(this.data.options)
    if (options[index].value_time > options[index].value_leavetime) {
      console.log("请重新选择")
      options[index].value_leavetime = ""
      this.setData({
        options: options
      })
      wx.showToast({
        icon: "none",
        title: '到达时间大于离开时间，请重新选择时间',
        duration: 2000
      })

    }
  },
  bindRegionChange: function (e) {
    console.log(e)
    let index = e.currentTarget.dataset.id //索引位置
    let value = e.detail.value //输入的内容
    let options = this.data.options
    // console.log(options)
    options[index].value_region = value
    this.setData({
      options: options
    })
    console.log(this.data.options[0].value_region)
  },
  //删除选项
  delOption(e) {
    let index = e.currentTarget.dataset.id //索引位置
    console.log(this.data.options)
    let options = this.data.options
    options.splice(index, 1)
    this.setData({
      options: options
    })
    console.log(this.data.options)
  },
  bindMultiPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      multiIndex: e.detail.value
    })
  },
  leavestyle_change:function(e){
    this.setData({
      leavestyle_choose:this.data.leavestyle[e.detail.value]
    })
    console.log(this.data.leavestyle_choose)
  },
  leaveway_change:function(e){
    this.setData({
      leaveway_choose:this.data.leaveway[e.detail.value]
    })
    console.log(this.data.leaveway_choose)
  },

  bindMultiPickerColumnChange: function (e) {
    var that = this;
    console.log('修改的列为', e.detail.column, '，值为', e.detail.value);
    var data = {
      multiArray: this.data.multiArray,
      multiIndex: this.data.multiIndex
    };
    data.multiIndex[e.detail.column] = e.detail.value;
    switch (e.detail.column) {
      case 0:
        switch (data.multiIndex[0]) {
          case 0:
            data.multiArray[1] = [];
            break;
          case 1:
            data.multiArray[1] = [];
            break;
          case 2:
            data.multiArray[1] = [''];
            break;
          case 3:
            data.multiArray[1] = [''];
            break;
          case 4:
            data.multiArray[1] = [''];
            break;
          case 5:
            data.multiArray[1] = [''];
            break;
          case 6:
            data.multiArray[1] = [''];
            break;
          case 7:
            data.multiArray[1] = [''];
            break;
        }
        data.multiIndex[1] = 0;
        break;
        data.multiIndex[2] = 0;
        console.log(data.multiIndex);
        break;
    }
    this.setData(data);
    this.setData({
      workshop: this.data.multiArray[0][this.data.multiIndex[0]],
      line: this.data.multiArray[1][this.data.multiIndex[1]],
    });

  },
  datareset:function(){
    this.setData({
      workshop:"",
      line:"",
      leaveway_choose:"",
      _leavestyle_choose:"",
      // peoplenumber:"",
      // username:"",
      options: [{
        "value": "",
        "value_time": "",
        "value_leavetime": "",
        "value_region": []
      }], //选项列表
      phonenumber:"",
      multiArray: [
        [''],
        ['']
      ],
      multiIndex: [0, 0],
      energySelect:"",
    })
  },
  getData: function (e) {
    wx.showLoading({
      title: '上传数据',
    })
    var getdata = this.data;
    var TIME = util.formatTime(new Date())
    db.collection("ApplyContent").add({
      data: {
        _workshop: getdata.workshop,
        _line: getdata.line,
        _peoplenumber: getdata.peoplenumber,
        _time: TIME,
        _username: getdata.username,
        _options: getdata.options,
        _phonenumber: getdata.phonenumber,
        _energySelect: getdata.energySelect,
        _leaveway_choose: getdata.leaveway_choose,
        _leavestyle_choose: getdata.leavestyle_choose,
      }
    }).then(res => {
      wx.hideLoading();
      wx.showToast({
        title: '已提交',
        icon: 'success',
        duration: 2000
      })
      this.datareset();
      // console.log("添加至数据库成功", res._id)
    }).catch(res => {
      console.log("添加失败", res)
      wx.showToast({
        title: '提交失败，请重试',
        icon: 'success',
        duration: 2000
      })
    })
  },
  submit: function () {
    var that = this.data;
    console.log("输出",that.options)
    if (that.username === "") {
      wx.showToast({
        title: '姓名未填写',
        icon: 'none',
        duration: 1500,
      })
    } else if (that.phonenumber == '') {
      wx.showToast({
        title: '工号未填写',
        icon: 'none',
        duration: 1500,
      })
    } else if (that.workshop == '') {
      wx.showToast({
        title: '部门未填写',
        icon: 'none',
        duration: 1500,
      })
    } else if (that.leavestyle_choose == '') {
      wx.showToast({
        title: '出行类型未填写',
        icon: 'none',
        duration: 1500,
      })
    } else if (that.leaveway_choose == '') {
      wx.showToast({
        title: '出行方式未填写',
        icon: 'none',
        duration: 1500,
      })
    } else if (that.options[0].region == '') {
      wx.showToast({
        title: '离沪地点未填写',
        icon: 'none',
        duration: 1500,
      })
    } 
    else if (that.options[0].value_time == '') {
      wx.showToast({
        title: '离沪时间未填写',
        icon: 'none',
        duration: 1500,
      })
    } 
      else if (that.options[0].value_leavetime == "") {
        wx.showToast({
          title: '离开时间未填写',
          icon: 'none',
          duration: 1500,
        })}
      else {
      this.getData();
    }
  },
  exportFile: function () {
    let that = this;
    //读取users表数据,用云函数进行数据库操作
    wx.cloud.callFunction({
      name: "getContentDataFunc",
      success(res) {
        console.log("读取成功", res.result.data)
        wx.showToast({
          title: '读取成功',
          icon: 'success',
          duration: 5000
        })
        that.saveExcel(res.result.data)
      },
      fail(res) {
        console.log("读取失败", res)
      }
    })
  },

  saveExcel: function (userdata) {
    let that = this
    wx.showLoading({
      title: '加载中',
    })
    wx.cloud.callFunction({
      name: "exportExcelFunc",
      data: {
        userdata: userdata
      },
      success(res) {
        console.log("保存成功", res)
        // console.log("保存成功", userdata[0]._options[0].value_region)
        wx.navigateTo({
          url: '../index/excelURL?url=' + res.result.fileID,
        })
        wx.hideLoading();
      },
      fail(res) {
        console.log("失败", res)
        wx.showToast({
          title: '保存失败，请联系管理员',
          icon: 'none',
          duration: 2000

        })
        // console.log("保存失败，请联系管理员", res)
      }
    })

  },
})